package com.hm.health.Activity.ecg;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.goodix.utils.SPUtils;
import com.goodix.widget.AlertDialogEx;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hm.health.Activity.BaseActivity;
import com.hm.health.R;
import com.hm.health.View.EcgHistoryRecordView;
import com.hm.health.entity.dao.EcgStatistics;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2020/3/6.
 */

public class EcgHistoryDetailActivity extends BaseActivity {

    private static final String TAG = "EcgDetail";
    public static final String EXT_ECG_HISTORY_DETAIL_INFO = "EXT_ECG_HISTORY_DETAIL_INFO";

    @BindView(R.id.chart_ecg)
    EcgHistoryRecordView mECGChartView;
    @BindView(R.id.tv_starttime)
    TextView mTvStartTime;
    @BindView(R.id.tv_duration)
    TextView mTvDuration;
    @BindView(R.id.tvPathologicalInformation)
    TextView mTvPathologicalInformation;
    @BindView(R.id.tvHrAverage)
    TextView mTvHrAverage;

    private EcgStatistics mStatistics;
    private List<Float> mDatas = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ecg_history_detail);
        ButterKnife.bind(this);
        initActionBar(getString(R.string.ecg_chart_detail));
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        String jsonStr = getIntent().getStringExtra(EXT_ECG_HISTORY_DETAIL_INFO);
        if (null != jsonStr) {
            mStatistics = new Gson().fromJson(jsonStr, new TypeToken<EcgStatistics>(){}.getType());
        }

        if (null == mStatistics) {
            AlertDialogEx.Builder builder = new AlertDialogEx.Builder(mContext);
            builder.setMessage(getString(R.string.failed_get_test_records));
            builder.setNegativeButton(R.string.quit, view -> finish());
            builder.show();
        }

        mTvStartTime.setText(mStatistics.getStartTime());
        mTvDuration.setText(String.format(getString(R.string.ecg_duration_format), mStatistics.getDuration()));
        mTvDuration.setVisibility(View.INVISIBLE);

        String[] pathological = getResources().getStringArray(R.array.pathological);

        int type = mStatistics.getPathologicalInformation();
        if (type < pathological.length) {
            mTvPathologicalInformation.setText(pathological[type]);
        } else {
            mTvPathologicalInformation.setText(pathological[0]);
        }

        mTvHrAverage.setText(String.format("%d BPM", mStatistics.getHrAverage()));

        loadEcgData();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_export:
                onExportBtnClicked(item.getActionView());
                break;
            case R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void onExportBtnClicked(View actionView) {
        /// Todo
        Intent intent = new Intent(mContext, ExportEcgToPdfActivity.class);
        intent.putExtra(EXT_ECG_HISTORY_DETAIL_INFO, new Gson().toJson(mStatistics));
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.ecg_detail_info, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void loadEcgData() {
        String filePath = mStatistics.getEcgRawdataPath();
        ArrayList<Integer> datas = SPUtils.getJsonObj(mContext, filePath, new ArrayList<>(),
                new TypeToken<List<Integer>>(){}.getType());
        for (int i = 0; i < datas.size(); ++i) {
            float v = datas.get(i) / 100f;
            v = v > 2f ? 2f : v;
            v = v < -1.5f ? -1.5f : v;
            mDatas.add(v);
            mECGChartView.addPoint(v);
        }

        refreshPlot();
    }

    private void refreshPlot() {
        if (mStatistics.getFrequence() <= 0) {
            mECGChartView.setFrequency(500);
        } else {
            mECGChartView.setFrequency(mStatistics.getFrequence());
        }
        mECGChartView.setEnableDrag(true);
        mECGChartView.setYAxisMaxValue(2f);
        mECGChartView.setYAxisMinValue(-1.5f);
        mECGChartView.invalidate();
    }
}
